//
//  BIDAnnouncementListViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/26.
//  Copyright © 2017年 mal. All rights reserved.
//

static NSString *listURL = @"townAndroid!gainAnnounceList.action";

#import "BIDAnnouncementListViewController.h"
#import "BIDAnnouncementDetailViewController.h"

@interface BIDAnnouncementListViewController ()<BIDBaseListViewDelegate>

@end

@implementation BIDAnnouncementListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"公告管理";
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], listURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
        }
        [self.myTableView reloadData];
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    cell.textLabel.text = dic[@"title"];
    cell.textLabel.textColor = RGBACOLOR(50, 54, 59, 1);
    cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"[%@]", dic[@"createTime"]];
    cell.detailTextLabel.textColor = RGBACOLOR(71, 70, 68, 1);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BIDAnnouncementDetailViewController *vc = [[BIDAnnouncementDetailViewController alloc] initWithNibName:@"BIDAnnouncementDetailViewController" bundle:nil];
    vc.announcementID = self.dataSourceArr[indexPath.row][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.f;
}

@end
